<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    实现“羊了个羊”消消乐（react） |  Shimmer
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/myBlog/images/ayer.svg" />
  
  
<link rel="stylesheet" href="/myBlog/dist/main.css">

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  
<link rel="stylesheet" href="/myBlog/css/custom.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

<link rel="alternate" href="/myBlog/atom.xml" title="Shimmer" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    
      
      <canvas width="1777" height="841"
        style="position: fixed; left: 0px; top: 0px; z-index: 99999; pointer-events: none;"></canvas>
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-2022-09-19-实现“羊了个羊”消消乐（react）"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  实现“羊了个羊”消消乐（react）
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/myBlog/2022/09/19/2022-09-19-%E5%AE%9E%E7%8E%B0%E2%80%9C%E7%BE%8A%E4%BA%86%E4%B8%AA%E7%BE%8A%E2%80%9D%E6%B6%88%E6%B6%88%E4%B9%90%EF%BC%88react%EF%BC%89/" class="article-date">
  <time datetime="2022-09-18T16:00:00.000Z" itemprop="datePublished">2022-09-19</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.8k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">14 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="实现“羊了个羊”消消乐（react）"><a href="#实现“羊了个羊”消消乐（react）" class="headerlink" title="实现“羊了个羊”消消乐（react）"></a>实现“羊了个羊”消消乐（react）</h3><p>体验地址：<a target="_blank" rel="noopener" href="https://code.juejin.cn/pen/7143974476500172814?mode=light">https://code.juejin.cn/pen/7143974476500172814?mode=light</a><br>和文章的代码写的有点不同。</p>
<h4 id="1、前言"><a href="#1、前言" class="headerlink" title="1、前言"></a>1、前言</h4><p>其实在羊了个羊出来的很久之前，我女朋友经常玩的一款游戏叫《方块物语》,就是一样的玩法，如下图。</p>
<img src="https://s1.ax1x.com/2022/09/19/x9nDpj.jpg" alt="x9nDpj.jpg" style="width:200px;height:400px" />

<p>当时看到的时候，就想着怎么去实现。也只是在脑海中构思了一下，没有动手行动起来。</p>
<p>然后看到了羊了个羊。诶，这不是和我女朋友之前玩的游戏一样吗？正好手头没什么事。就尝试了实现了一下。</p>
<h4 id="2、实现"><a href="#2、实现" class="headerlink" title="2、实现"></a>2、实现</h4><h5 id="1、开始准备"><a href="#1、开始准备" class="headerlink" title="1、开始准备"></a>1、开始准备</h5><p>由于懒得去搭项目，正好自己有个平时学习的react项目，就加个路由用react实现。</p>
<p>首先是布局，分为上中下三个部分，标题，游戏区域，操作区域。</p>
<p>就不说了。</p>
<h5 id="2、游戏区域生成、是否被覆盖的判断。"><a href="#2、游戏区域生成、是否被覆盖的判断。" class="headerlink" title="2、游戏区域生成、是否被覆盖的判断。"></a>2、游戏区域生成、是否被覆盖的判断。</h5><p>初始数据，这里用的是12个文字,</p>
<p><code>name:</code>文字</p>
<p><code>color:</code>背景颜色</p>
<p><code>position:</code>对应的位置分别是<code>[第几层,第几行,第几个]</code></p>
<p>由于想偷懒，我直接每个同时放了三个。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> gameItemsArr = [</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;马&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#5470c6&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;马&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#5470c6&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;马&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#5470c6&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;驰&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#fac858&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;驰&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#fac858&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;驰&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#fac858&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;朱&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#ee6666&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;朱&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#ee6666&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;朱&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#ee6666&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;振&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#73c0de&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;振&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#73c0de&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;振&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#73c0de&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;骚&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#3bd272&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;骚&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#3bd272&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;骚&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#3bd272&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;刘&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#9a60b4&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;刘&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#9a60b4&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;刘&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#9a60b4&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;胖&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#ea7ccc&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;胖&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#ea7ccc&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;胖&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#ea7ccc&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;邢&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#c14cac&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;邢&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#c14cac&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;邢&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#c14cac&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;吊&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#f08300&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;吊&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#f08300&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;吊&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#f08300&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;龙&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#004eff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;龙&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#004eff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;龙&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#004eff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;楞&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#00e4ff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;楞&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#00e4ff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;楞&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#00e4ff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;张&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#c0e8ff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;张&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#c0e8ff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">    &#123;<span class="attr">name</span>: <span class="string">&#x27;张&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;#c0e8ff&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;,</span><br><span class="line">];</span><br></pre></td></tr></table></figure>
<p><strong>接下来是生成游戏区域的items</strong></p>
<p>这边简单说一下level当前的关卡。一共设置了6个关卡。</p>
<p><code>floorItemsArr:</code>每层有多少个，第一层9个，3的平方，第二层16个,4的平方依次类推，第一关有两层。</p>
<p>每层就是个正方形。</p>
<p>因为还要有空缺。这边也是偷懒，每个等级有多少空的。剩下的数组是三的倍数就可以了。</p>
<p>然后从上面的<code>gameItemsArr</code>取数据，看看不是空的<code>item</code>是不是36的倍数。</p>
<p>这边图省事也是写死的。</p>
<p>然后和空<code>item</code>数组拼接一下。</p>
<p>使用<code>sort(()=&gt;MathMath.random() - 0.5)</code>打乱</p>
<p>然后依次从<code>allItems</code>取出每层的<code>item</code></p>
<p>为了显示层级效果最上层的<code>z-index</code>值最大。这里也是偷懒写死的</p>
<p>最后判断每个元素的位置</p>
<p>每层的宽度和<code>z-index</code>。</p>
<p>第一关最后就得到了一个这样的数组。</p>
<p><a target="_blank" rel="noopener" href="https://imgse.com/i/pScPu36"><img src="https://s1.ax1x.com/2023/02/06/pScPu36.png" alt="pScPu36.png"></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 生成游戏items</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> itemsArr = <span class="function">(<span class="params">level</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> arr = [...gameItemsArr];</span><br><span class="line">    <span class="comment">// 每层有多少item</span></span><br><span class="line">    <span class="keyword">const</span> floorItemsArr = [<span class="number">9</span>, <span class="number">16</span>, <span class="number">25</span>, <span class="number">36</span>, <span class="number">49</span>, <span class="number">64</span>, <span class="number">81</span>];</span><br><span class="line">    <span class="keyword">const</span> floorNumsArr = [<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 每个等级有多少空item数组</span></span><br><span class="line">    <span class="keyword">const</span> noItemsNum = [<span class="number">7</span>, <span class="number">14</span>, <span class="number">14</span>, <span class="number">24</span>, <span class="number">49</span>, <span class="number">40</span>];</span><br><span class="line">    <span class="comment">// 每个等级有多少item数组</span></span><br><span class="line">    <span class="keyword">const</span> itemsNum = [<span class="number">18</span>, <span class="number">36</span>, <span class="number">72</span>, <span class="number">111</span>, <span class="number">150</span>, <span class="number">240</span>];</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 空item数组</span></span><br><span class="line">    <span class="keyword">const</span> noItemsArr = [];</span><br><span class="line">    <span class="keyword">let</span> i = noItemsNum[level - <span class="number">1</span>];</span><br><span class="line">    <span class="keyword">while</span> (i &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        noItemsArr.push(&#123;<span class="attr">name</span>: <span class="string">&#x27;&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;&#x27;</span>, <span class="attr">position</span>: [<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]&#125;);</span><br><span class="line">        i--;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 最后不完整的取几个</span></span><br><span class="line">    <span class="keyword">const</span> getArrItems = ((itemsNum[level - <span class="number">1</span>] / <span class="number">3</span>) % <span class="number">12</span>) * <span class="number">3</span>;</span><br><span class="line">    <span class="keyword">const</span> otherItemsArr = arr</span><br><span class="line">        .map(<span class="function">(<span class="params">i, index</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (index &lt; getArrItems) &#123;</span><br><span class="line">                <span class="keyword">return</span> i;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">        &#125;)</span><br><span class="line">        .filter(<span class="function">(<span class="params">i</span>) =&gt;</span> i);</span><br><span class="line">    <span class="keyword">let</span> itemsNumsArr = [];</span><br><span class="line">    <span class="keyword">if</span> (level === <span class="number">1</span>) &#123;</span><br><span class="line">        itemsNumsArr = otherItemsArr;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (level === <span class="number">2</span>) &#123;</span><br><span class="line">        itemsNumsArr = arr;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (level === <span class="number">3</span>) &#123;</span><br><span class="line">        itemsNumsArr = arr.concat(arr);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (level === <span class="number">4</span>) &#123;</span><br><span class="line">        arr = [...gameItemsArr];</span><br><span class="line">        itemsNumsArr = arr.concat(arr, arr, otherItemsArr);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (level === <span class="number">5</span>) &#123;</span><br><span class="line">        arr = [...gameItemsArr];</span><br><span class="line">        itemsNumsArr = arr.concat(arr, arr, arr, otherItemsArr);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (level === <span class="number">6</span>) &#123;</span><br><span class="line">        arr = [...gameItemsArr];</span><br><span class="line">        itemsNumsArr = arr.concat(arr, arr, arr, arr, arr, otherItemsArr);</span><br><span class="line">    &#125;</span><br><span class="line">   <span class="comment">//打乱数组</span></span><br><span class="line">    <span class="keyword">const</span> allItems = itemsNumsArr.concat(noItemsArr).sort(<span class="function">() =&gt;</span> <span class="built_in">Math</span>.random() - <span class="number">0.5</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> resultArr = [];</span><br><span class="line"></span><br><span class="line">    floorItemsArr.splice(<span class="number">0</span>, level + <span class="number">1</span>).map(<span class="function">(<span class="params">item, fIndex</span>) =&gt;</span> &#123;</span><br><span class="line">        resultArr.push(allItems.splice(<span class="number">0</span>, item));</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> zIndex = [<span class="number">6</span>, <span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">2</span>, <span class="number">1</span>];</span><br><span class="line">    <span class="keyword">return</span> resultArr.map(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            arr: [</span><br><span class="line">                ...item.map(<span class="function">(<span class="params">i, j</span>) =&gt;</span> &#123;</span><br><span class="line">                    i.position = [index + <span class="number">1</span>, <span class="built_in">parseInt</span>(j / floorNumsArr[index]) + <span class="number">1</span>, (j + <span class="number">1</span>) % floorNumsArr[index] || floorNumsArr[index]];</span><br><span class="line">                    <span class="keyword">return</span> &#123;...i&#125;;</span><br><span class="line">                &#125;),</span><br><span class="line">            ],</span><br><span class="line">            zIndex: zIndex[index],</span><br><span class="line">            width: (index + <span class="number">3</span>) * <span class="number">35</span>,</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>得到了数组以后，就可以渲染页面了,但是还要解决覆盖的问题。</p>
<p>像上面那样，每一个上一层的元素覆盖了下一层的4个元素。</p>
<p>上面我们设置的<code>position</code>就排上了用场。</p>
<p>例如第一层的第一个位置是<code>[1,1,1]</code>,那它所覆盖的元素就是<code>[2,1,1],[2,1,2],[2,2,1],[2,2,2]</code></p>
<p>以此类推循环判断，就得到了一个都是<code>position</code>数组。</p>
<p>然后通过<code>findIndex</code>或者<code>filter</code>判断当前点击的<code>item</code>在不在这个数组当中。</p>
<p>在就不能点击，不在就可以点击。</p>
<p><strong>为了能点击到下层，有个这个属性<code>pointer-events:none</code>设置给父元素，点击可以穿透，为了子元素可以点击。item设置<code>pointer-events:auto</code></strong></p>
<p>代码如下。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> isItemCanClick = <span class="function">(<span class="params">item, allItems</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> nowItem = &#123;...item&#125;;</span><br><span class="line">    <span class="keyword">const</span> nowItemFloor = nowItem.position[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">const</span> upFloor = allItems[nowItemFloor - <span class="number">2</span>]?.arr.filter(<span class="function">(<span class="params">itemNow</span>) =&gt;</span> itemNow.name !== <span class="string">&#x27;&#x27;</span>) || [];</span><br><span class="line">    <span class="comment">// 最底下层不判断</span></span><br><span class="line">    <span class="keyword">if</span> (upFloor.length === <span class="number">0</span>) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">let</span> midArr = [];</span><br><span class="line">    upFloor.map(<span class="function">(<span class="params">itemNowM</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">const</span> position = itemNowM.position;</span><br><span class="line">        <span class="keyword">const</span> a = [position[<span class="number">0</span>] + <span class="number">1</span>, position[<span class="number">1</span>], position[<span class="number">2</span>]];</span><br><span class="line">        <span class="keyword">const</span> b = [position[<span class="number">0</span>] + <span class="number">1</span>, position[<span class="number">1</span>], position[<span class="number">2</span>] + <span class="number">1</span>];</span><br><span class="line">        <span class="keyword">const</span> c = [position[<span class="number">0</span>] + <span class="number">1</span>, position[<span class="number">1</span>] + <span class="number">1</span>, position[<span class="number">2</span>]];</span><br><span class="line">        <span class="keyword">const</span> d = [position[<span class="number">0</span>] + <span class="number">1</span>, position[<span class="number">1</span>] + <span class="number">1</span>, position[<span class="number">2</span>] + <span class="number">1</span>];</span><br><span class="line">        midArr.push(a);</span><br><span class="line">        midArr.push(b);</span><br><span class="line">        midArr.push(c);</span><br><span class="line">        midArr.push(d);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> isCanClick =</span><br><span class="line">        midArr.filter(<span class="function">(<span class="params">mItem</span>) =&gt;</span> nowItem.position[<span class="number">0</span>] === mItem[<span class="number">0</span>] &amp;&amp; nowItem.position[<span class="number">1</span>] === mItem[<span class="number">1</span>] &amp;&amp; nowItem.position[<span class="number">2</span>] === mItem[<span class="number">2</span>])</span><br><span class="line">            .length === <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> isCanClick;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><strong>这样就可以完美生成我们的游戏区域了</strong></p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">&lt;div className=<span class="string">&quot;gameArea&quot;</span>&gt;</span><br><span class="line">               &#123;gameItemsArr.map(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">                   <span class="keyword">return</span> (</span><br><span class="line">                       &lt;div</span><br><span class="line">                           className=<span class="string">&quot;gameAreaFloor&quot;</span></span><br><span class="line">                           key=&#123;item.zIndex&#125;</span><br><span class="line">                           style=&#123;</span><br><span class="line">                               item.arr.filter(<span class="function">(<span class="params">k</span>) =&gt;</span> k.name !== <span class="string">&#x27;&#x27;</span>).length !== <span class="number">0</span></span><br><span class="line">                                   ? &#123;<span class="attr">zIndex</span>: item.zIndex, <span class="attr">width</span>: item.width, <span class="attr">height</span>: item.width&#125;</span><br><span class="line">                                   : &#123;<span class="attr">display</span>: <span class="string">&#x27;none&#x27;</span>&#125;</span><br><span class="line">                           &#125;&gt;</span><br><span class="line">                           &#123;item.arr.map(<span class="function">(<span class="params">gameItem, gameIndex</span>) =&gt;</span> &#123;</span><br><span class="line">                               <span class="keyword">return</span> (</span><br><span class="line">                                   &lt;div</span><br><span class="line">                                       onClick=&#123;<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                                           <span class="keyword">if</span> (gameItem.name === <span class="string">&#x27;&#x27;</span> || !isItemCanClick(gameItem, gameItemsArr)) <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">                                           moveItem(gameItem, gameIndex, index);</span><br><span class="line">                                       &#125;&#125;</span><br><span class="line">                                       key=&#123;gameIndex&#125;</span><br><span class="line">                                       style=&#123;</span><br><span class="line">                                           gameItem.name === <span class="string">&#x27;&#x27;</span></span><br><span class="line">                                               ? &#123;<span class="attr">opacity</span>: <span class="number">0</span>, <span class="attr">pointerEvents</span>: <span class="string">&#x27;none&#x27;</span>&#125;</span><br><span class="line">                                               : isItemCanClick(gameItem, gameItemsArr)</span><br><span class="line">                                               ? &#123;<span class="attr">background</span>: gameItem.color&#125;</span><br><span class="line">                                               : &#123;<span class="attr">background</span>: <span class="string">&#x27;#c2cbcbee&#x27;</span>&#125;</span><br><span class="line">                                       &#125;</span><br><span class="line">                                       className=<span class="string">&quot;gameItem&quot;</span>&gt;</span><br><span class="line">                                       &#123;gameItem.name&#125;</span><br><span class="line">                                   &lt;/div&gt;</span><br><span class="line">                               );</span><br><span class="line">                           &#125;)&#125;</span><br><span class="line">                       &lt;/div&gt;</span><br><span class="line">                   );</span><br><span class="line">               &#125;)&#125;</span><br><span class="line">           &lt;/div&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h5 id="3、操作判断"><a href="#3、操作判断" class="headerlink" title="3、操作判断"></a>3、操作判断</h5><p>这里就比较简单，判断当前点击的层数，和点击的<code>item</code>。</p>
<p>把<code>item  push</code>到操作的数组里面。</p>
<p>通过<code>splice</code>删除当前点击的元素并添加一个<code>name为&#39;&#39;</code>的<code>item</code></p>
<p>再判断当前点击的元素在不在操作的数组里，在就找到存在的数组，放到一起，不在就放到前面，</p>
<p>有三个一样就清除。都是通过<code>findIndex</code>和<code>splice</code></p>
<p>当前层级都清除完了，就进入下一关  <code>level+1</code>,重新生成游戏区域。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> React, &#123;useState, useEffect&#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;itemsArr&#125; <span class="keyword">from</span> <span class="string">&#x27;./content&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./page.css&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123;isItemCanClick&#125; <span class="keyword">from</span> <span class="string">&#x27;./action&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> hasThreeItem = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">const</span> GameBox = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 分数</span></span><br><span class="line">    <span class="keyword">const</span> [score, setScore] = useState(<span class="number">0</span>);</span><br><span class="line">    <span class="comment">// 第几关</span></span><br><span class="line">    <span class="keyword">const</span> [level, setLevel] = useState(<span class="number">1</span>);</span><br><span class="line">    <span class="comment">// 游戏的item</span></span><br><span class="line">    <span class="keyword">const</span> [gameItemsArr, setGameItemsArr] = useState(itemsArr(level));</span><br><span class="line">    <span class="comment">// 操作的数组</span></span><br><span class="line">    <span class="keyword">const</span> [operateArr, setOperateArr] = useState([<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]);</span><br><span class="line">    <span class="comment">// 当前点击的item，撤回用</span></span><br><span class="line">    <span class="keyword">const</span> [nowClickItemInfo, setNowClickItemInfo] = useState([<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">null</span>]);</span><br><span class="line">    <span class="comment">// 操作次数</span></span><br><span class="line">    <span class="keyword">const</span> [isOperate, setIsOperate] = useState([<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">    useEffect(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (operateArr.filter(<span class="function">(<span class="params">item</span>) =&gt;</span> item).length === <span class="number">7</span>) &#123;</span><br><span class="line">            alert(<span class="string">&#x27;游戏失败！&#x27;</span>);</span><br><span class="line">            <span class="built_in">window</span>.location.reload();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;, [operateArr]);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 判断游戏是否下一关</span></span><br><span class="line">    useEffect(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (gameItemsArr.length === <span class="number">0</span>) &#123;</span><br><span class="line">            alert(<span class="string">`恭喜你通过第<span class="subst">$&#123;level&#125;</span>关了！！！`</span>);</span><br><span class="line">            <span class="keyword">if</span> (level === <span class="number">6</span>) &#123;</span><br><span class="line">                alert(<span class="string">&#x27;恭喜你通关了！！！&#x27;</span>);</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            setLevel(<span class="function">(<span class="params">prev</span>) =&gt;</span> prev + <span class="number">1</span>);</span><br><span class="line">            setGameItemsArr(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">                prev = itemsArr(level + <span class="number">1</span>);</span><br><span class="line">                <span class="keyword">return</span> [...prev];</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;, [gameItemsArr, level]);</span><br><span class="line">    <span class="comment">// 点击item</span></span><br><span class="line">    <span class="keyword">const</span> moveItem = <span class="function">(<span class="params">item, index, whichFloor</span>) =&gt;</span> &#123;</span><br><span class="line">        hasThreeItem = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">let</span> hasThreeindex = <span class="number">0</span>;</span><br><span class="line">        <span class="comment">// 记录当前的点击item信息</span></span><br><span class="line">        <span class="keyword">const</span> nowClickItem = [item, index, whichFloor];</span><br><span class="line">        setNowClickItemInfo(nowClickItem);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 放到操作区</span></span><br><span class="line">        setOperateArr(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">const</span> currentIndex = prev.findIndex(<span class="function">(<span class="params">k</span>) =&gt;</span> k &amp;&amp; k.name === item.name);</span><br><span class="line">            prev.pop();</span><br><span class="line">            <span class="keyword">if</span> (currentIndex &gt; -<span class="number">1</span>) &#123;</span><br><span class="line">                prev.splice(currentIndex, <span class="number">0</span>, item);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                prev.unshift(item);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> [...prev];</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="comment">// 操作原始数组</span></span><br><span class="line">        setGameItemsArr(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">            prev[whichFloor].arr.splice(index, <span class="number">1</span>, &#123;...item, <span class="attr">name</span>: <span class="string">&#x27;&#x27;</span>, <span class="attr">color</span>: <span class="string">&#x27;&#x27;</span>&#125;);</span><br><span class="line">            <span class="keyword">if</span> (prev[prev.length - <span class="number">1</span>].arr.filter(<span class="function">(<span class="params">item</span>) =&gt;</span> item.name !== <span class="string">&#x27;&#x27;</span>).length === <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> [];</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> [...prev];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 判断清除逻辑</span></span><br><span class="line">        operateArr.map(<span class="function">(<span class="params">newItem, newIndex</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (newItem &amp;&amp; newItem.name === item.name) &#123;</span><br><span class="line">                hasThreeItem++;</span><br><span class="line">                hasThreeindex = newIndex;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 如果有三个一样的</span></span><br><span class="line">        <span class="keyword">if</span> (hasThreeItem === <span class="number">2</span>) &#123;</span><br><span class="line">            setOperateArr(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">                prev.splice(hasThreeindex - <span class="number">1</span>, <span class="number">3</span>);</span><br><span class="line">                prev.push(<span class="literal">null</span>);</span><br><span class="line">                prev.push(<span class="literal">null</span>);</span><br><span class="line">                prev.push(<span class="literal">null</span>);</span><br><span class="line">                <span class="keyword">return</span> [...prev];</span><br><span class="line">            &#125;);</span><br><span class="line">            setScore(<span class="function">(<span class="params">prev</span>) =&gt;</span> prev + <span class="number">100</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">        &lt;div className=<span class="string">&quot;gamebox&quot;</span>&gt;</span><br><span class="line">            &#123;<span class="comment">/* 标题区域 */</span>&#125;</span><br><span class="line">            &lt;div className=<span class="string">&quot;titleArea&quot;</span>&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;titleAreaTitle&quot;</span>&gt;汉字方块&lt;/div&gt;</span><br><span class="line">                &lt;div className=<span class="string">&quot;titleAreaScore&quot;</span>&gt;</span><br><span class="line">                    &lt;span&gt;分数:&lt;/span&gt;</span><br><span class="line">                    &lt;span&gt;&#123;score&#125;&lt;/span&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">            &#123;<span class="comment">/* 游戏区域 */</span>&#125;</span><br><span class="line">            &lt;div className=<span class="string">&quot;gameArea&quot;</span>&gt;</span><br><span class="line">                &#123;gameItemsArr.map(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">                    <span class="keyword">return</span> (</span><br><span class="line">                        &lt;div</span><br><span class="line">                            className=<span class="string">&quot;gameAreaFloor&quot;</span></span><br><span class="line">                            key=&#123;item.zIndex&#125;</span><br><span class="line">                            style=&#123;</span><br><span class="line">                                item.arr.filter(<span class="function">(<span class="params">k</span>) =&gt;</span> k.name !== <span class="string">&#x27;&#x27;</span>).length !== <span class="number">0</span></span><br><span class="line">                                    ? &#123;<span class="attr">zIndex</span>: item.zIndex, <span class="attr">width</span>: item.width, <span class="attr">height</span>: item.width&#125;</span><br><span class="line">                                    : &#123;<span class="attr">display</span>: <span class="string">&#x27;none&#x27;</span>&#125;</span><br><span class="line">                            &#125;&gt;</span><br><span class="line">                            &#123;item.arr.map(<span class="function">(<span class="params">gameItem, gameIndex</span>) =&gt;</span> &#123;</span><br><span class="line">                                <span class="keyword">return</span> (</span><br><span class="line">                                    &lt;div</span><br><span class="line">                                        onClick=&#123;<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                                            <span class="keyword">if</span> (gameItem.name === <span class="string">&#x27;&#x27;</span> || !isItemCanClick(gameItem, gameItemsArr)) <span class="keyword">return</span>;</span><br><span class="line"></span><br><span class="line">                                            moveItem(gameItem, gameIndex, index);</span><br><span class="line">                                        &#125;&#125;</span><br><span class="line">                                        key=&#123;gameIndex&#125;</span><br><span class="line">                                        style=&#123;</span><br><span class="line">                                            gameItem.name === <span class="string">&#x27;&#x27;</span></span><br><span class="line">                                                ? &#123;<span class="attr">opacity</span>: <span class="number">0</span>, <span class="attr">pointerEvents</span>: <span class="string">&#x27;none&#x27;</span>&#125;</span><br><span class="line">                                                : isItemCanClick(gameItem, gameItemsArr)</span><br><span class="line">                                                ? &#123;<span class="attr">background</span>: gameItem.color&#125;</span><br><span class="line">                                                : &#123;<span class="attr">background</span>: <span class="string">&#x27;#c2cbcbee&#x27;</span>&#125;</span><br><span class="line">                                        &#125;</span><br><span class="line">                                        className=<span class="string">&quot;gameItem&quot;</span>&gt;</span><br><span class="line">                                        &#123;gameItem.name&#125;</span><br><span class="line">                                    &lt;/div&gt;</span><br><span class="line">                                );</span><br><span class="line">                            &#125;)&#125;</span><br><span class="line">                        &lt;/div&gt;</span><br><span class="line">                    );</span><br><span class="line">                &#125;)&#125;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line"></span><br><span class="line">            &#123;<span class="comment">/* 下方格子 */</span>&#125;</span><br><span class="line">            &lt;div className=<span class="string">&quot;operateArea&quot;</span>&gt;</span><br><span class="line">                &#123;operateArr.map(<span class="function">(<span class="params">item, index</span>) =&gt;</span> &#123;</span><br><span class="line">                    <span class="keyword">if</span> (!item) &#123;</span><br><span class="line">                        <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">key</span>=<span class="string">&#123;index&#125;</span> <span class="attr">className</span>=<span class="string">&quot;gameItemOperate&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="keyword">return</span> (</span><br><span class="line">                            &lt;div key=&#123;index&#125; style=&#123;&#123;<span class="attr">background</span>: item.color&#125;&#125; className=<span class="string">&quot;gameItemOperate&quot;</span>&gt;</span><br><span class="line">                                &#123;item.name&#125;</span><br><span class="line">                            &lt;/div&gt;</span><br><span class="line">                        );</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)&#125;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">            &lt;div className=<span class="string">&quot;operateBtnArr&quot;</span>&gt;</span><br><span class="line">                &lt;span</span><br><span class="line">                    className=<span class="string">&quot;operateBtn&quot;</span></span><br><span class="line">                    style=&#123;isOperate[<span class="number">0</span>] === <span class="number">1</span> ? &#123;<span class="attr">background</span>: <span class="string">&#x27;grey&#x27;</span>&#125; : &#123;&#125;&#125;</span><br><span class="line">                    onClick=&#123;<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                        <span class="keyword">if</span> (isOperate[<span class="number">0</span>] === <span class="number">1</span>) <span class="keyword">return</span>;</span><br><span class="line">                        setGameItemsArr(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">                            prev = itemsArr(level);</span><br><span class="line">                            <span class="keyword">return</span> [...prev];</span><br><span class="line">                        &#125;);</span><br><span class="line">                        setIsOperate(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">                            prev[<span class="number">0</span>] = <span class="number">1</span>;</span><br><span class="line">                            <span class="keyword">return</span> [...prev];</span><br><span class="line">                        &#125;);</span><br><span class="line">                    &#125;&#125;&gt;</span><br><span class="line">                    随机</span><br><span class="line">                &lt;/span&gt;</span><br><span class="line">                &lt;span</span><br><span class="line">                    className=<span class="string">&quot;operateBtn&quot;</span></span><br><span class="line">                    style=&#123;isOperate[<span class="number">1</span>] === <span class="number">1</span> ? &#123;<span class="attr">background</span>: <span class="string">&#x27;grey&#x27;</span>&#125; : &#123;&#125;&#125;</span><br><span class="line">                    onClick=&#123;<span class="function">() =&gt;</span> &#123;</span><br><span class="line">                        <span class="keyword">if</span> (isOperate[<span class="number">1</span>] === <span class="number">1</span>) <span class="keyword">return</span>;</span><br><span class="line">                        setOperateArr(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">                            <span class="keyword">const</span> lastIndex = prev.findIndex(<span class="function">(<span class="params">item</span>) =&gt;</span> item &amp;&amp; item.name === nowClickItemInfo[<span class="number">0</span>].name);</span><br><span class="line">                            prev.splice(lastIndex, <span class="number">1</span>);</span><br><span class="line">                            <span class="keyword">return</span> [...prev];</span><br><span class="line">                        &#125;);</span><br><span class="line">                        setGameItemsArr(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">                            prev[nowClickItemInfo[<span class="number">2</span>]].arr[nowClickItemInfo[<span class="number">1</span>]] = nowClickItemInfo[<span class="number">0</span>];</span><br><span class="line">                            <span class="keyword">return</span> [...prev];</span><br><span class="line">                        &#125;);</span><br><span class="line"></span><br><span class="line">                        setIsOperate(<span class="function">(<span class="params">prev</span>) =&gt;</span> &#123;</span><br><span class="line">                            prev[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">                            <span class="keyword">return</span> [...prev];</span><br><span class="line">                        &#125;);</span><br><span class="line">                    &#125;&#125;&gt;</span><br><span class="line">                    撤回</span><br><span class="line">                &lt;/span&gt;</span><br><span class="line">                &lt;span className=<span class="string">&quot;operateBtn&quot;</span>&gt;加三个格子&lt;/span&gt;</span><br><span class="line">            &lt;/div&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">    );</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> GameBox;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><a href="%E6%B8%B8%E6%88%8F%E7%95%8C%E9%9D%A2"><img src="https://s1.ax1x.com/2023/02/06/pScPKgK.png" alt="pScPKgK.png"></a></p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://gitee.com/shimmer612/myBlog/2022/09/19/2022-09-19-%E5%AE%9E%E7%8E%B0%E2%80%9C%E7%BE%8A%E4%BA%86%E4%B8%AA%E7%BE%8A%E2%80%9D%E6%B6%88%E6%B6%88%E4%B9%90%EF%BC%88react%EF%BC%89/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/myBlog/tags/React/" rel="tag">React</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/myBlog/2022/10/26/2022-10-26-Rollup%E6%89%93%E5%8C%85%E5%BA%93%E6%96%87%E4%BB%B6/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Rollup打包库文件
          
        </div>
      </a>
    
    
      <a href="/myBlog/2022/09/09/2022-09-09-%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9B%B8%E5%90%8C%E5%9C%B0%E5%9D%80%E4%B8%8D%E5%90%8C%E6%A0%87%E7%AD%BE%E9%A1%B5%E7%9A%84localStorage%E3%80%81sessionStorage%E3%80%81cookie/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">浏览器相同地址不同标签页的`localStorage、sessionStorage、cookie`</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "xGZKFxfEhEyqsK7YA4NNoQNr-gzGzoHsz",
    app_key: "qCe4kOh0W9rCgNWcv31cO3NF",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "说出你想说的吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <!--
 * @Author: zhangzhen
 * @Date: 2023-02-04 10:30:09
 * @LastEditors: zhangzhen
 * @LastEditTime: 2023-05-23 09:47:12
 * 
-->
<footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2021-2023
        <i class="ri-heart-fill heart_icon"></i> Shimmer
      </li>
    </ul>
    <!-- <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul> -->
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/myBlog/"><img src="/myBlog/images/ayer-side.svg" alt="Shimmer"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/archives">目录</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/2021/02/12/2021-02-12-%E5%85%B3%E4%BA%8E%E6%88%91/">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/myBlog/message">留言板</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/myBlog/js/jquery-2.0.3.min.js"></script>


<script src="/myBlog/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/myBlog/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/myBlog/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/myBlog/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->


<script src="/myBlog/js/clickBoom2.js"></script>


<!-- CodeCopy -->


<link rel="stylesheet" href="/myBlog/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>